package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.common.util.UpqcSecurityUtil;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.ComInfo;
import cc.rengu.igas.channel.upqc.facade.bean.CouponInfoBean;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.B2CTranStateQueryRequest;
import cc.rengu.igas.channel.upqc.facade.response.B2CTranStateQueryResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import java.util.List;
import java.util.Map;


/**
 * 收款交易状态查询
 * AC->UP
 * reqType:0540000903
 */
public class B2CTransStateQueryService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        B2CTranStateQueryRequest b2CTranStateQueryRequest = new B2CTranStateQueryRequest();
        ConvertUtil.convertOutput(b2CTranStateQueryRequest);
        return b2CTranStateQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        B2CTranStateQueryResponse b2CTranStateQueryResponse = new B2CTranStateQueryResponse();
        B2CTranStateQueryRequest b2CTranStateQueryRequest = (B2CTranStateQueryRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(b2CTranStateQueryRequest);
        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, b2CTranStateQueryRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, b2CTranStateQueryRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            /*调用银联服务超时，返回调用方超时，调用方继续发起查询*/
            rglog.error("调用银联二维码收款查证服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        } else if (0 != iReturnCode) {
            /*调用服务失败需要做*/
            rglog.error("调用银联二维码收款查证服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(b2CTranStateQueryResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(b2CTranStateQueryResponse.getRespCode())) {
            b2CTranStateQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            b2CTranStateQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            chnlBizResponse.setResult(b2CTranStateQueryResponse);
            return chnlBizResponse;
        }
        if (b2CTranStateQueryResponse.getSettleDate() != null) {
            b2CTranStateQueryResponse.setSettleDate(UpqcSecurityUtil.getCupsSettleDate(b2CTranStateQueryResponse.getSettleDate()));
        }
        String payInfoEnc = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.payInfoStr);

        if (payInfoEnc != null) {
            String payInfo = descryptData(payInfoEnc);
            rglog.error("解密的payInfo:<{}>", payInfo);
            b2CTranStateQueryResponse.setPayerInfo((PayerInfo) convertKvStr2Bean(payInfo.substring(1, payInfo.length() - 1), PayerInfo.class));
        }

        String comInfoStr = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.comInfoStr);
        if (comInfoStr != null) {
            comInfoStr = new String(Base64.decodeBase64(comInfoStr));
            rglog.error("解密的comInfo:<{}>", comInfoStr);
            ComInfo comInfo = new ComInfo();
            Map<String, Object> kvMap = UpqcSecurityUtil.parseKvString(comInfoStr.substring(1, comInfoStr.length() - 1));
            if (kvMap.get("F0") != null) {
                comInfo.setMsgType((String) kvMap.get("F0"));
            }
            if (kvMap.get("F3") != null) {
                comInfo.setProcCode((String) kvMap.get("F3"));
            }
            if (kvMap.get("F25") != null) {
                comInfo.setEntryMode((String) kvMap.get("F25"));
            }
            if (kvMap.get("F37") != null) {
                comInfo.setReferenceNo((String) kvMap.get("F37"));
            }
            if (kvMap.get("F60") != null) {
                comInfo.setReserve((String) kvMap.get("F60"));
            }
            b2CTranStateQueryResponse.setComInfo(comInfo);
        }

        if (xmlTreeUtil.isExist(UpqcTreeNodeConstant.couponInfoStr)) {
            String couponInfoStr = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.couponInfoStr);
            List<CouponInfoBean> couponInfoBeanList = JSON.parseArray(new String(Base64.decodeBase64(couponInfoStr)), CouponInfoBean.class);
            for (CouponInfoBean couponInfoBean : couponInfoBeanList) {
                if (couponInfoBean.getOffstAmt() != null) {
                    couponInfoBean.setOffstAmt(AmountUtil.changeF2Y(couponInfoBean.getOffstAmt()));
                }
            }
            b2CTranStateQueryResponse.setCouponInfoList(couponInfoBeanList);
        }

        chnlBizResponse.setResult(b2CTranStateQueryResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        B2CTranStateQueryResponse b2CTranStateQueryResponse = (B2CTranStateQueryResponse) response.getResult();
        ConvertUtil.convertInput(b2CTranStateQueryResponse);
    }

}
